package com.sunnypower.rushfitapp.BodyDataPart;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sunnypower.rushfitapp.R;
import com.sunnypower.rushfitapp.db.BodyData;
import com.sunnypower.rushfitapp.util.ParseStringToOther;

public class AddBodyData extends Activity {
    private EditText new_height;
    private EditText new_weight;
    private EditText new_waist;
    private EditText new_hipline;
    private EditText new_chest;
    private EditText new_thigh;
    private Button save_body_data;
    private static final String TAG = "AddBodyData";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_body_data);
        // 初始化控件
        new_height = findViewById(R.id.new_height);
        new_weight = findViewById(R.id.new_weight);
        new_waist = findViewById(R.id.new_waist);
        new_hipline = findViewById(R.id.new_hipline);
        new_chest = findViewById(R.id.new_chest);
        new_thigh = findViewById(R.id.new_thigh);
        save_body_data = findViewById(R.id.save_body_data);
        save_body_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: |" + (new_height.getText().toString().trim().equals("")) + "|");
                //判断用户是否输入了所有数据
                if (new_height.getText().toString().trim().equals("") || new_weight.getText().toString().trim().equals("")
                        || new_waist.getText().toString().trim().equals("") || new_hipline.getText().toString().trim().equals("")
                        || new_chest.getText().toString().trim().equals("") || new_thigh.getText().toString().trim().equals("")) {
                    Toast.makeText(getApplicationContext(), "请输入所有数据！", Toast.LENGTH_SHORT).show();
                } else {
                    // 获取用户性别
                    int sex = getIntent().getIntExtra("sex", 1);
                    // 获取数据封装进对象
                    BodyData bodyData = new BodyData();
                    bodyData.setHeight(ParseStringToOther.strToint(new_height.getText().toString()));
                    bodyData.setWeight(ParseStringToOther.strToFloat(new_weight.getText().toString()));
                    bodyData.setWaist(ParseStringToOther.strToFloat(new_waist.getText().toString()));
                    bodyData.setHipline(ParseStringToOther.strToFloat(new_hipline.getText().toString()));
                    bodyData.setChest(ParseStringToOther.strToFloat(new_chest.getText().toString()));
                    bodyData.setThigh(ParseStringToOther.strToFloat(new_thigh.getText().toString()));
                    bodyData.setFatRate(countFatRate(ParseStringToOther.strToFloat(new_waist.getText().toString()),
                            ParseStringToOther.strToFloat(new_weight.getText().toString()), sex));
                    // 返回数据给上一个活动
                    Intent intent = new Intent();
                    intent.putExtra("new_data", bodyData);
                    Log.d(TAG, "onClick: " + bodyData);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });
    }

    /**
     * 计算体脂率
     *
     * @param waist  胸围
     * @param weight 体重
     * @return
     */
    private Float countFatRate(Float waist, Float weight, int sex) {
        Float result;
        if (sex == 0) {// 女性
            Float parm_a = waist * 0.74F;
            Float parm_b = weight * 0.082F + 34.89F;
            result = (parm_a - parm_b) / weight;
        } else {// 男性
            Float parm_a = waist * 0.74F;
            Float parm_b = weight * 0.082F + 44.74F;
            result = (parm_a - parm_b) / weight;
        }
        return result;
    }
}
